//
//  BattleAnimationController.m
//  basicMMMRPG
//
//  Created by Simon André on 15/01/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CharacterBattleAnimationController.h"
#import "CharacterViewController.h"
#import "CharacterProtocol.h"
#import "BattleProtocol.h"
#import "BattleViewController.h"
#import "Constants.h"


@implementation CharacterBattleAnimationController

@synthesize animationIsExecuting;
@synthesize character,animationIncrement,currentMoveOrigin,currentMoveDestination,currentMoveEndSelector,currentMoveEndSelectorTarget,completedMoveSelector;
@synthesize originalPosition,targetPosition,attackIsInPhaseOne;

-(id) initWithCharacter:(CharacterViewController*) AnimatedCharacter{
	if (self = [super init]) {
		[self setCharacter:AnimatedCharacter];
		[self setAnimationIsExecuting:NO];
		[self setAnimationIncrement:moveIncrement];
	}
	return self;
}

-(void) executeAttackToPoint:(CGPoint) targetPoint{
	if (![self animationIsExecuting]) {
		[self setAnimationIsExecuting:YES];
		[self setOriginalPosition:[[[self character] view] center]];
		[self setTargetPosition:targetPoint];
		[self executeAttackToTargetInitlalMove];
	}
}

-(void) executeAttackToTarget:(UIViewController*) target{
	if (![self animationIsExecuting]) {
		[self setAnimationIsExecuting:YES];
		[self setOriginalPosition:[[[self character] view] center]];
		[self setTargetPosition:[[target view] center]];
		[self executeAttackToTargetInitlalMove];
	}
}

-(void) executeAttackToTargetInitlalMove{
	[self setAttackIsInPhaseOne:YES];
	[self setCurrentMoveOrigin:[self originalPosition]];
	[self setCurrentMoveDestination:[self targetPosition]];
	[self setCompletedMoveSelector:@selector(executeAttackToTargetAttackAction)];
	[self executeMoveIncrement];
}

-(void) chanceAttackdestination:(CGPoint) newTargetPosition{
	if ([self attackIsInPhaseOne] && [self animationIsExecuting]) {
		[self setCurrentMoveDestination:newTargetPosition];
		[self setTargetPosition:newTargetPosition];
	}
}

-(void) executeAttackToTargetAttackAction{
	[self setAttackIsInPhaseOne:NO];
	[[self character] animateAttackOnceWithSelectorSpriteAnimationEnded:@selector(executeAttackToTargetBackToInitialPosition) onTarget:self];
}

-(void) executeAttackToTargetBackToInitialPosition{
	[[[[[self character] character] currentBattle] controller] attackInflictDamage];
	[self setCurrentMoveOrigin:[self targetPosition]];
	[self setCurrentMoveDestination:[self originalPosition]];
	[self setCompletedMoveSelector:@selector(endOfAttack)];
	[self executeMoveIncrement];
}

-(void) endOfAttack{
	[[self character] animateStand];
	[self setAnimationIsExecuting:NO];
	[[self character] setActionEnded];
}

-(void) executeMoveIncrement{
	CGPoint currentPosition = [[[self character] view] center];
	CGPoint destination = [self currentMoveDestination];
	if ((!(currentPosition.x == destination.x)) || (!(currentPosition.y == destination.y))) {
		SEL selectorSetMoveOrientation = @selector(setMoveOrientation);
		SEL selectorIncrementEnded = @selector(executeMoveIncrement);
		CGFloat distX = destination.x-currentPosition.x;
		CGFloat distY = destination.y-currentPosition.y;
		CGFloat incrementDestinationX;
		CGFloat incrementDestinationY;
		if(distX > [self animationIncrement]){
			incrementDestinationX = currentPosition.x+[self animationIncrement];
		}
		else if(distX < -[self animationIncrement]){
			incrementDestinationX = currentPosition.x-[self animationIncrement];
		}
		else {
			incrementDestinationX = destination.x;
		}
		
		if(distY > [self animationIncrement]){
			incrementDestinationY = currentPosition.y+[self animationIncrement];
		}
		else if(distY < -[self animationIncrement]){
			incrementDestinationY = currentPosition.y-[self animationIncrement];
		}
		else {
			incrementDestinationY = destination.y;
		}
		CGPoint incrementDestination = CGPointMake(incrementDestinationX, incrementDestinationY);
		[UIView beginAnimations:nil context:NULL];
		[UIView setAnimationDelegate:self];
		[UIView setAnimationWillStartSelector:selectorSetMoveOrientation];
		[UIView setAnimationDidStopSelector:selectorIncrementEnded];
		[UIView setAnimationDuration:moveIncrementDuration];
		[[[self character] view] setCenter:incrementDestination];
		[UIView commitAnimations];
	}
	else {
		[self performSelector:[self completedMoveSelector]];
	}
	
}

-(void) executeMoveToDestination:(CGPoint) destination withActionAtEnd:(SEL)endSelector onTarget:(id) endSelectorTarget{
	if ([self animationIsExecuting]) {
		[self setCurrentMoveOrigin:[[[self character] view] center]];
		[self setCurrentMoveDestination:destination];
		[self setCurrentMoveEndSelector:endSelector];
		[self setCompletedMoveSelector:@selector(executeMoveToDestinationEnd)];
		[self setCurrentMoveEndSelectorTarget:endSelectorTarget];
		[self setAnimationIsExecuting:YES];
		[self executeMoveIncrement];
	}
}

-(void) setMoveOrientation{
	if ([self currentMoveOrigin].x < [self currentMoveDestination].x) {
		[[self character] animateMoveRight];
	}
	else if([self currentMoveOrigin].x > [self currentMoveDestination].x) {
		[[self character] animateMoveLeft];
	}
	else {
		[[self character] animateStand];
	}


}

-(void) executeMoveToDestinationEnd{
	[self setAnimationIsExecuting:YES];
	[[self currentMoveEndSelectorTarget] performSelector:[self currentMoveEndSelector]];
}

- (void) dealloc{
	[self setAnimationIsExecuting:NO];
	[self setCharacter:nil];
	[self setCurrentMoveEndSelectorTarget:nil];
	[super dealloc];
}

@end
